package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import csu.web.credit_bank.mapper.CourseResourcesMapper;
import csu.web.credit_bank.pojo.CourseResource;
import csu.web.credit_bank.service.CourseResourcesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseResourcesServiceImpl extends ServiceImpl<CourseResourcesMapper, CourseResource> implements CourseResourcesService {

    @Autowired
    private UUIDManager uuidManager;

    @Override
    public CourseResource createLink(String courseId, String resourceId) {
        String linkId = uuidManager.getUUID();
        if (linkId == null) {
            throw new RuntimeException("ID池已空，无法创建关联.");
        }
        CourseResource link = new CourseResource();
        link.setId(linkId);
        link.setCourseId(courseId);
        link.setResourceId(resourceId);
        this.save(link);
        return link;
    }

    @Override
    public List<CourseResource> getLinksByCourseId(String courseId) {
        QueryWrapper<CourseResource> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        return this.list(wrapper);
    }

    @Override
    public CourseResource updateLink(String linkId, String newResourceId) {
        CourseResource link = this.getById(linkId);
        if (link == null) {
            throw new RuntimeException("未找到要更新的关联记录，ID: " + linkId);
        }
        link.setResourceId(newResourceId);
        this.updateById(link);
        return link;
    }

    @Override
    public boolean deleteLinkById(String linkId) {
        return this.removeById(linkId);
    }

    @Override
    public boolean deleteLinkByCourseAndResource(String courseId, String resourceId) {
        QueryWrapper<CourseResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId).eq("resource_id", resourceId);
        return this.remove(queryWrapper);
    }

    @Override
    public CourseResource createOrUpdateLinkWithChapter(String courseId, String resourceId, int chapter) {
        QueryWrapper<CourseResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId).eq("resource_id", resourceId);
        CourseResource existingLink = this.getOne(queryWrapper);

        if (existingLink != null) {
            // 如果已存在，更新章节号
            existingLink.setChapter(chapter);
            this.updateById(existingLink);
            return existingLink;
        } else {
            // 如果不存在，创建新的关联
            String linkId = uuidManager.getUUID();
            if (linkId == null) {
                throw new RuntimeException("ID池已空，无法分配新ID，创建课程资源关联失败");
            }
            CourseResource newLink = new CourseResource();
            newLink.setId(linkId);
            newLink.setCourseId(courseId);
            newLink.setResourceId(resourceId);
            newLink.setChapter(chapter);
            this.save(newLink);
            return newLink;
        }
    }

    @Override
    public int getMaxChapterByCourseId(String courseId) {
        QueryWrapper<CourseResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId)
                .orderByDesc("chapter")
                .last("LIMIT 1");

        CourseResource maxChapterResource = this.getOne(queryWrapper);
        return maxChapterResource != null ? maxChapterResource.getChapter() : 0;
    }

}